package com.ziqing.apeletnews.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;
import com.ziqing.apeletnews.R;
import com.ziqing.apeletnews.activity.ReMindActivity;
import com.ziqing.apeletnews.adapter.HotAdapter;
import com.ziqing.apeletnews.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by WT on 2018/4/12.
 */

public class HomeFragment extends BaseFragment {
    @BindView(R.id.home_banner)
    Banner homeBanner;
    Unbinder unbinder;
    @BindView(R.id.iv_home_news_bg_one)
    ImageView ivHomeNewsBgOne;
    @BindView(R.id.iv_home_news_bg_two)
    ImageView ivHomeNewsBgTwo;
    @BindView(R.id.iv_home_news_bg_three)
    ImageView ivHomeNewsBgThree;
    @BindView(R.id.tl_tab)
    TabLayout tlTab;
    @BindView(R.id.vp_pager)
    ViewPager vpPager;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.iv_remind)
    ImageView iv_remind;
    @BindView(R.id.iv_right)
    ImageView iv_right;

    private HotAdapter adapter;
    private List<String> data = new ArrayList<>();
    private String[] mTitles = new String[]{"最新", "产品经理", "Web前端", "Java"};


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, view);
        rvList.setNestedScrollingEnabled(false);
        homeBanner.setFocusable(true);
        homeBanner.setFocusableInTouchMode(true);
        homeBanner.requestFocus();
        initBanner();
        Glide.with(getContext())
                .load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523540008011&di=787efacfa7c9ffe22f055cf798f4adca&imgtype=0&src=http%3A%2F%2Fcrawl.nosdn.127.net%2F8d6ffc6437b8e1e61db526f65c84c210.jpg")
                .into(ivHomeNewsBgOne);
        Glide.with(getContext())
                .load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523540008011&di=787efacfa7c9ffe22f055cf798f4adca&imgtype=0&src=http%3A%2F%2Fcrawl.nosdn.127.net%2F8d6ffc6437b8e1e61db526f65c84c210.jpg")
                .into(ivHomeNewsBgTwo);
        Glide.with(getContext())
                .load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523540008011&di=787efacfa7c9ffe22f055cf798f4adca&imgtype=0&src=http%3A%2F%2Fcrawl.nosdn.127.net%2F8d6ffc6437b8e1e61db526f65c84c210.jpg")
                .into(ivHomeNewsBgThree);
        initTabView();
        initRecycleView();
        return view;
    }

    private void initRecycleView() {
        data.add("http://img.mp.itc.cn/upload/20170713/7cbc63330a424c7db6ebf59c583af040_th.jpg");
        data.add("http://img.mp.itc.cn/upload/20170713/7cbc63330a424c7db6ebf59c583af040_th.jpg");
        data.add("http://img.mp.itc.cn/upload/20170713/7cbc63330a424c7db6ebf59c583af040_th.jpg");
        data.add("http://img.mp.itc.cn/upload/20170713/7cbc63330a424c7db6ebf59c583af040_th.jpg");
        data.add("http://img.mp.itc.cn/upload/20170713/7cbc63330a424c7db6ebf59c583af040_th.jpg");
        data.add("http://img.mp.itc.cn/upload/20170713/7cbc63330a424c7db6ebf59c583af040_th.jpg");
        data.add("http://img.mp.itc.cn/upload/20170713/7cbc63330a424c7db6ebf59c583af040_th.jpg");
        data.add("http://img.mp.itc.cn/upload/20170713/7cbc63330a424c7db6ebf59c583af040_th.jpg");
        data.add("http://img.mp.itc.cn/upload/20170713/7cbc63330a424c7db6ebf59c583af040_th.jpg");
        data.add("http://img.mp.itc.cn/upload/20170713/7cbc63330a424c7db6ebf59c583af040_th.jpg");

        rvList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        adapter = new HotAdapter(getActivity(), data, R.layout.item_recycle_home);
        rvList.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }


    private void initTabView() {
        vpPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                if (position == 0) {
                    return new RecentlyFragment();
                } else {
                    return new RecentlyFragment();
                }
            }

            @Override
            public int getCount() {
                return mTitles.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles[position];
            }
        });
        tlTab.setupWithViewPager(vpPager);
        vpPager.setOffscreenPageLimit(mTitles.length);

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initBanner() {
        List<String> imgs = new ArrayList<>();
        imgs.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523534858291&di=b2a8296583f10590a779879e29ea3c46&imgtype=0&src=http%3A%2F%2Fwww.segahome.com%2Fimages%2Fcollect_article%2F26397%2F14951630772424.jpg");
        imgs.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523535419948&di=6d8775189845e41f0240d48e799cae42&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D943898424%2C1950347304%26fm%3D214%26gp%3D0.jpg");
        imgs.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523535361149&di=cc36420987bd46ad733b5e92872d8249&imgtype=0&src=http%3A%2F%2Fimg0.pconline.com.cn%2Fpconline%2F1306%2F09%2F3336552_143T2JU-0.jpg");
        homeBanner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        homeBanner.setImages(imgs);
        //设置轮播时间
        homeBanner.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        homeBanner.setIndicatorGravity(BannerConfig.RIGHT);
        //设置banner动画效果
//        homeBanner.setBannerAnimation(Transformer.CubeOut);
        //设置点击事件
        homeBanner.setOnBannerClickListener(position -> {

        });

        //banner设置方法全部调用完毕时最后调用
        homeBanner.start();
    }

    @OnClick(R.id.iv_remind)
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_remind) {
          startActivity(new Intent(getContext(), ReMindActivity.class));
        }
    }

    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            //具体方法内容自己去选择，次方法是为了减少banner过多的依赖第三方包，所以将这个权限开放给使用者去选择
            Glide.with(context.getApplicationContext())
                    .load(path)
                    .crossFade()
                    .into(imageView);
        }
    }
}
